#include<bits/stdc++.h>
#define int long long

using namespace std;

signed main() {
	int n, ret = 0x3f3f3f3f;
	cin >> n;
	string s;
	cin >> s;
	int a[n + 10];
	for(int i = 1; i <= n; i ++ ) {
		cin >> a[i];
	}
	
	int y[n + 10], e[n + 10], l[n + 10];
	y[0] = 0;
	e[0] = 0;
	l[0] = 0;
	
	for(int i = 1; i <= n; i ++ ) {
		y[i] = y[i - 1];
		e[i] = e[i - 1];
		l[i] = l[i - 1];
		if(s[i - 1] != '1') {
			y[i] += a[i];
		}
		if(s[i - 1] != '0') {
			l[i] += a[i];
		}
		if(s[i - 1] != '2'){
			e[i] += a[i];
		}
	}
	
	int x1[n + 10], x2[n + 10], x3[n + 10], x4[n + 10], x5[n + 10], x6[n + 10];
	x1[0] = x2[0] = x3[0] = x4[0] = x5[0] = x6[0] = 0;
	
	for(int i = 1; i <= n; i ++ ) {
		for(int j = i + 1; j <= n; j ++ ) {
			ret = min(ret, l[i] + y[j] - y[i] + e[n] - e[j]);
			ret = min(ret, l[i] + e[j] - e[i] + y[n] - y[j]);
			ret = min(ret, y[i] + l[j] - l[i] + e[n] - e[j]);
			ret = min(ret, y[i] + e[j] - e[i] + l[n] - l[j]);
			ret = min(ret, e[i] + y[j] - y[i] + l[n] - l[j]);
			ret = min(ret, e[i] + l[j] - l[i] + y[n] - y[j]);
		}
	} 
	cout << ret << endl;
	return 0;
}
